package com.css.fxfzxczh.modules.lossAssessment.floorArea.repository.impl;

import com.css.fxfzxczh.modules.lossAssessment.floorArea.entity.FloorArea;
import com.css.fxfzxczh.modules.lossAssessment.floorArea.parm.FloorAreaParam;
import com.css.fxfzxczh.modules.lossAssessment.floorArea.repository.FloorAreaRepository;
import com.css.fxfzxczh.modules.lossAssessment.resetPrice.entity.ResetPrice;
import com.css.fxfzxczh.util.PlatformObjectUtils;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/02/23
 * @Description: 确定总建筑面积
 */
@Repository
public class FloorAreaRepositoryImpl implements FloorAreaRepository {
    @Resource
    JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    private static final String TABLE_NAME = "xczh_floor_area"; // 表名
    private static final String TABLE_ID = "id"; // 表id字段名

    @Override
    public Map<String, Object> getPage(int curPage, int pageSize, FloorAreaParam param) {
        StringBuilder sql = new StringBuilder("select * from " + TABLE_NAME);
        StringBuilder whereSql = new StringBuilder(" where del_flag = '0'");
        StringBuilder orderSql = new StringBuilder(" order by area,structure_type desc");
        int firstIndex = (curPage - 1) * pageSize;
        int lastIndex = pageSize;
        StringBuilder limitSql = new StringBuilder(" limit " + lastIndex + " offset " + firstIndex);
        //地震事件id
        if (!PlatformObjectUtils.isEmpty(param.getXsemId())) {
            whereSql.append(" and xsem_id = '" + param.getXsemId() + "'");
        }
        //评估子区
        if (!PlatformObjectUtils.isEmpty(param.getArea())) {
            whereSql.append(" and area = '" + param.getArea() + "'");
        }
        //结构类型
        if (!PlatformObjectUtils.isEmpty(param.getStructureType())) {
            whereSql.append(" and structure_type = '" + param.getStructureType() + "'");
        }
        sql.append(whereSql).append(orderSql);
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(ResetPrice.class)).size();
        List<FloorArea> list = jdbcTemplate.query(sql.append(limitSql).toString(), new BeanPropertyRowMapper<>(FloorArea.class));
        Map<String, Object> map = new HashMap();
        map.put("total", total);
        map.put("rows", list);
        return map;
    }

    @Override
    public void updateById(FloorArea param) {
        String sql = "update " + TABLE_NAME +
                " set gross_floor_area = :grossFloorArea,update_user = :updateUser,update_time = :updateTime " +
                "where del_flag = '0' and " + TABLE_ID + " = '" + param.getId() + "'";
        SqlParameterSource source = new BeanPropertySqlParameterSource(param);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public FloorArea findById(String id) {
        String sql = "select * from " + TABLE_NAME + " where " + TABLE_ID + " = '" + id + "'";
        try {
            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(FloorArea.class));
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    @Override
    public void deleteById(List<String> ids) {
        String sql = "update " + TABLE_NAME + " set del_flag = '1'  where " + TABLE_ID + " in (:ids)";
        Map idMap = new HashMap();
        idMap.put("ids", ids);
        namedParameterJdbcTemplate.update(sql, idMap);
    }

    @Override
    public void batchSave(List<FloorArea> list) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(list.toArray());
        String sql = "INSERT INTO "+TABLE_NAME+" (id,xsem_id,area,structure_type," +
                "gross_floor_area,reporter,reporter_time,del_flag,create_user,create_time)" +
                "values (:id,:xsemId,:area,:structureType," +
                ":grossFloorArea,:reporter,:reporterTime,:delFlag,:createUser,:createTime)";
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }

    @Override
    public List<String> getEvaluationSubarea(String xsemId) {
        String sql ="select evaluation_subarea_name from xczh_survey_task_manage where del_flag = '0' and xsem_id = '" + xsemId + "'";
        return jdbcTemplate.queryForList(sql, String.class);
    }

    @Override
    public String getName(String formatCellValue) {
        String sql ="select evaluation_subarea_name from xczh_survey_task_manage where del_flag = '0' and evaluation_subarea_name = '" + formatCellValue + "'";
        try {
            return jdbcTemplate.queryForObject(sql, String.class);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }
}
